package org.kfive.nova.thread;

import java.util.concurrent.*;

/**
 * Created by admin on 2017-04-13.
 */
public class MultiThread {

    class CalculationThread implements Callable<Integer>{
        private Integer counter;
        public CalculationThread(Integer initCounter){
            this.counter = initCounter;
        }
        @Override
        public Integer call() throws Exception {
            for (;;){
                counter++;
                System.out.println(counter);
                Thread.sleep(1000);
                if(counter>=1000){
                    break;
                }

            }
            return counter;
        }
    }

    public void test(){
        ExecutorService executor = Executors.newSingleThreadExecutor();
        CountDownLatch latch = new CountDownLatch(1);
        CalculationThread calculationThread = new CalculationThread(1);
        Future<Integer> future = executor.submit(calculationThread);
        try {
            Integer counter = future.get();
            System.out.println("result="+counter);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        MultiThread multiThread = new MultiThread();
        multiThread.test();
    }
}
